use std::collections::HashMap;
use std::env;
use std::fs::canonicalize;
use std::path::PathBuf;

#[derive(Debug, Default, Clone)]
pub struct BmbpLibPom {
    pub(crate) group: String,
    pub arti: String,
    pub version: String,
    pub pom_file: String,
    pub(crate) jar_file: String,
}

pub fn walk_pom_lib(base_dir: &String, local_type: &String) -> Result<Vec<BmbpLibPom>, String> {
    match local_type.to_lowercase().as_str() {
        "gradle" => walk_pom_gradle_lib(base_dir),
        _ => walk_pom_maven_lib(base_dir),
    }
}

fn walk_pom_gradle_lib(base_dir: &String) -> Result<Vec<BmbpLibPom>, String> {
    let mut repo_path = base_dir.trim().clone();
    if repo_path.is_empty() {
        repo_path = ".";
    }
    println!("==read repo_path:{}", repo_path);
    if repo_path.ends_with("files-2.1")
        || repo_path.ends_with("files-2.1/")
        || repo_path.ends_with("files-2.1\\")
    {
        let path_buf = PathBuf::from(repo_path);
        let pom_vec = walk_nest_file(&path_buf)?;
        println!("===> pom_vec:{}", pom_vec.len());
        return Ok(pom_vec);
    }
    Err("GRADLE请指定files-2.1文件夹所在位置".to_string())
}

fn walk_nest_file(cache_dir_path: &PathBuf) -> Result<Vec<BmbpLibPom>, String> {
    if cache_dir_path.is_file() {
        return Err(format!("{}需为文件夹", cache_dir_path.to_str().unwrap()));
    }
    let mut pom_vec = vec![];
    // 获取GROUP
    let group_dir = cache_dir_path.read_dir().unwrap();
    for group_dir_item in group_dir {
        let group_item = group_dir_item.unwrap();
        if group_item.path().is_file() {
            continue;
        }
        let group_os = group_item.file_name();
        let group = group_os.to_str().unwrap();
        // 读取arti
        let arti_dir = group_item.path().read_dir().unwrap();
        for arti_dir_item in arti_dir {
            let arti_item = arti_dir_item.unwrap();
            if arti_item.path().is_file() {
                continue;
            }
            let arti_name_os = arti_item.file_name();
            let arti_name = arti_name_os.to_str().unwrap();

            let version_dir = arti_item.path().read_dir().unwrap();
            for version_dir_itm in version_dir {
                let version_item = version_dir_itm.unwrap();
                if version_item.path().is_file() {
                    continue;
                }

                let version_os = version_item.file_name();
                let version = version_os.to_str().unwrap();

                let cache_dir = version_item.path().read_dir().unwrap();
                for cache_dir_item in cache_dir {
                    let cache_item = cache_dir_item.unwrap();
                    if cache_item.path().is_file() {
                        continue;
                    }
                    // 实际文件
                    let file_dir = cache_item.path().read_dir().unwrap();
                    for file_dir_item in file_dir {
                        let file_item = file_dir_item.unwrap();
                        if file_item.path().is_dir() {
                            continue;
                        }
                        let mut pom_lib = BmbpLibPom {
                            group: group.to_string(),
                            arti: arti_name.to_string(),
                            version: version.to_string(),
                            pom_file: "".to_string(),
                            jar_file: "".to_string(),
                        };
                        let file_item_path = file_item.path();
                        let mut file_path = canonicalize(&file_item_path)
                            .unwrap()
                            .to_str()
                            .unwrap()
                            .to_string();
                        if std::env::consts::OS == "windows" && file_path.starts_with("\\\\?\\") {
                            file_path = file_path.replace("\\\\?\\", "");
                        }
                        println!("===>:{}", file_path);
                        let file_name_os = file_item.file_name();
                        let file_name = file_name_os.to_str().unwrap();

                        if file_name.ends_with(".jar") && !file_name.ends_with("sources.jar") {
                            pom_lib.jar_file = file_path
                        } else if file_name.ends_with(".pom") {
                            pom_lib.pom_file = file_path
                        }
                        pom_vec.push(pom_lib);
                    }
                }
            }
        }
    }
    Ok(pom_vec)
}

fn walk_pom_maven_lib(base_dir: &String) -> Result<Vec<BmbpLibPom>, String> {
    let vec = vec![];
    println!("===> 读取MAVEN仓库:{}", base_dir);
    Ok(vec)
}
